package com.chengh.nami.common.response;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

/**
 * @author 通用实体类
 * @param <T> 枚举
 */
@Getter
@Setter
@Builder
@ToString
public class R<T> implements Serializable {

    private Integer code;

    private String message;

    private T data;

    public static <T> R<T> ok() {
       return R.<T>builder().code(ResponseCodeEnum.SUCCESS.getCode()).message(ResponseCodeEnum.SUCCESS.getMessage()).build();
    }

    public static <T> R<T> ok(T data) {
        R<T> ok = ok();
        ok.setData(data);
        return ok;
    }

    public static <T> R<T> ok(String message) {
        return R.<T>builder().code(ResponseCodeEnum.SUCCESS.getCode()).message(message).build();
    }

    public static <T> R<T> ok(Integer code, String message) {
        return R.<T>builder().code(code).message(message).build();
    }

    public static <T> R<T> ok(String message, T data) {
        return R.<T>builder().code(ResponseCodeEnum.SUCCESS.getCode()).message(message).data(data).build();
    }

    public static <T> R<T> failed() {
        return R.<T>builder().code(ResponseCodeEnum.ERROR.getCode()).message(ResponseCodeEnum.ERROR.getMessage()).build();
    }

    public static <T> R<T> failed(T data) {
        R<T> failed = R.failed();
        failed.setData(data);
        return failed;
    }

    public static <T> R<T> failed(Integer code, String message) {
        return R.<T>builder().code(code).message(message).build();
    }

    public static <T> R<T> failed(String message) {
        return R.<T>builder().code(ResponseCodeEnum.ERROR.getCode()).message(message).build();
    }

    public static <T> R<T> bool(boolean b) {
        if(b) {
            return R.ok();
        }
        return R.failed();
    }
}
